package modes

import (
	"fmt"
	"strconv"
)

/*
 * 描述: 课程资源
 * start 0启用 1禁用
 *****************************/

type SubClass struct {
	Id  int64 `json:"-" xorm:"id"`     //ID
	PId int64 `json:"pid" xorm:"p_id"` //项目ID
	CId int64 `json:"-" xorm:"c_id"`   //班ID
	SId int64 `json:"-" xorm:"s_id"`   //课程
	At  int64 `json:"-" xorm:"at"`     //时间

}
type JoinSubClassResource struct {
	SubClass `xorm:"extends"`
	Subject  `xorm:"extends"`
	Resource `xorm:"extends"`
}

type JoinTopName struct {
	SubClass  `xorm:"extends"`
	TopicName `xorm:"extends"`
	Count     int64 `json:"count" xorm:"-"` //题的总数
}

func (this *SubClass) TableName() string {
	return "c_subject"
}

func (this *SubClass) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *JoinSubClassResource) CountList(vid string) (int64, error) {
	where := fmt.Sprintf("subject.id >= %d and subject.id < %d and resources.url = '%s' and resources.s_type = 0 and resources.start = 0 ", this.SubClass.SId, this.SubClass.SId+1000, vid)
	return Db(0).Table(this.SubClass.TableName()).
		Join("INNER", "subject", fmt.Sprintf("%s.s_id = subject.id and subject.id >= %d and subject.id < %d ", this.SubClass.TableName(), this.SubClass.SId, this.SubClass.SId)).
		Join("INNER", "resources", fmt.Sprintf("%s.s_id = resources.sid", this.SubClass.TableName())).
		Where(where).
		Count(this)
}

func (this *SubClass) where(fage, count, page int, where, field string) ([]SubClass, error) {
	list := make([]SubClass, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

type JoinSubject struct {
	SubClass `xorm:"extends"`
	Subject  `xorm:"extends"`
}

func (this *JoinSubject) CLists(val *Pager) error {
	list := make([]JoinSubject, 0)
	val.TName = "c_subject"
	val.Page--
	if val.Page <= 0 {
		val.Page = 0
	}
	var field, sort string
	field = fmt.Sprintf("%s.s_id", val.TName) // 默认时间排序
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s desc", field)
	} else {
		sort = fmt.Sprintf("%s asc", field)
	}
	swhere := fmt.Sprintln("s.start = 0")
	//val.Total, _ = Db(0).Table(val.TName).
	//	Join("LEFT", "subject s", fmt.Sprintf("%s.s_id = s.id", val.TName)).
	//	Where(val.ToWhere(), swhere).
	//	Where(swhere).
	//	Count()

	err := Db(1).Table(val.TName).
		Join("LEFT", "subject s", fmt.Sprintf("%s.s_id = s.id", val.TName)).
		Where(val.ToWhere()).
		Where(swhere).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}

func (this *SubClass) Lookupss(val *Pager) error {
	list := make([]SubClass, 0)
	val.TName = "c_subject"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).
		Where(val.ToWhere()).
		Count(this)
	err := Db(0).Table(val.TName).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err

}

func (this *SubClass) PIdList(cid int64) ([]SubClass, error) {
	list := make([]SubClass, 0)
	var err error
	err = Db(1).Table("c_subject").
		Where("c_id = ?", cid).
		Find(&list)
	return list, err
}

func (this *SubClass) Lists(cid int64) ([]SubClass, error) {
	swhere := fmt.Sprintf("c_id = %d", cid)
	return this.where(1, 100000, 0, swhere, "")
}

func (this *SubClass) GetList(cidList []int) ([]SubClass, error) {
	list := make([]SubClass, 0)
	err := Db(1).Table("c_subject").In("c_id", cidList).Find(&list)
	return list, err
}
func (this *SubClass) GetCidsByCids(cidList []int) ([]int64, error) {
	list := make([]int64, 0)
	err := Db(1).Table("c_subject").Select("s_id-s_id%100000 as aa ").Distinct("s_id-s_id%100000").In("c_id", cidList).Find(&list)
	return list, err
}
func (this *SubClass) GetPidsByCids(cidList []int) ([]int64, error) {
	list := make([]int64, 0)
	err := Db(1).Table("c_subject").Select("p_id").Distinct("p_id").In("c_id", cidList).Find(&list)
	return list, err
}

func (this *SubClass) GetidsByCids(cidList []int) ([]int64, error) {
	list := make([]int64, 0)
	err := Db(1).Table("c_subject").Distinct("s_id").Select("s_id").In("c_id", cidList).Find(&list)
	return list, err
}

func (this *JoinTopName) TopName() ([]JoinTopName, error) {
	list := make([]JoinTopName, 0)
	selec := "SELECT DISTINCT(t.type_id) as type_id,c.id as id,t.id as n_id, c.p_id as p_id, c.c_id as c_id,c.s_id as s_id,t.name as name FROM c_subject AS c"
	inner := "INNER JOIN toplibname t ON c.s_id = t.sid"
	where := fmt.Sprintf("where c.c_id = %d and c.s_id > %d  and c.s_id < %d and t.type_id = %d ", this.CId, this.SubClass.SId, this.SubClass.SId+100000, this.TopicName.TypeId)
	orderby := "order by t.at"
	group := ""
	sil, err := Db(1).Query(fmt.Sprintf("%s %s %s %s %s", selec, inner, where, orderby, group))
	if nil == err {
		for i, _ := range sil {
			this.TopicName.Id, _ = strconv.ParseInt(string(sil[i]["n_id"]), 10, 64)
			this.TypeId, _ = strconv.Atoi(string(sil[i]["type_id"]))
			this.Name = string(sil[i]["name"])
			this.TopicName.SId, _ = strconv.ParseInt(string(sil[i]["s_id"]), 10, 64)
			this.Count = GetToplibCount(this.TopicName.SId, this.TopicName.Id)
			list = append(list, *this)
		}
	}
	fmt.Println(list)
	return list, err
}

func GetToplibCount(s_id, n_id int64) int64 {
	fmt.Println(s_id, n_id)
	var top TopicLib
	w := fmt.Sprintf("sid = %d and n_id = %d ", s_id, n_id)
	num, _ := top.Count(w)
	return num
}
